package com.example.usercenter.controller;

import com.example.usercenter.Exception.BusinsessException;
import com.example.usercenter.common.BaseResponse;
import com.example.usercenter.common.ErrorCode;
import com.example.usercenter.common.ResultUtils;
import com.example.usercenter.model.domain.Team;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.model.dto.TeamJoinQuery;
import com.example.usercenter.model.dto.TeamQuery;
import com.example.usercenter.model.dto.TeamUpdateQuery;
import com.example.usercenter.model.request.TeamAddRequest;
import com.example.usercenter.model.request.TeamQueryRequest;
import com.example.usercenter.model.vo.UserVo;
import com.example.usercenter.service.TeamService;
import com.example.usercenter.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;

    /**
     * 创建队伍
     * @Author:XueYuan
     */
    @PostMapping("/create")
    public BaseResponse<Boolean> createtTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request){
        if (teamAddRequest == null){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamAddRequest,team);
        boolean save = teamService.createTeam(team,request);
        return ResultUtils.success(save);
    }

    /**
     * 修改队伍
     * @Author:XueYuan
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTea(@RequestBody TeamUpdateQuery teamUpdateQuery,HttpServletRequest request){
        if (teamUpdateQuery == null){
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"传递参数为空");
        }
        boolean update = teamService.updateTeam(teamUpdateQuery,request);
        return ResultUtils.success(update);
    }

    /**
     * 获取队伍成员
     * @Author:XueYuan
     */
    @GetMapping("/getUser")
    public BaseResponse<List<UserVo>> listTeams(@RequestBody TeamQuery teamQuery,HttpServletRequest request){
        if (teamQuery == null){
            throw new BusinsessException(ErrorCode.PARAMS_ERROR);
        }
        boolean isadmin = userService.isAdmin(request);
        List<UserVo> userVos = teamService.listTeams(teamQuery,isadmin);
        return ResultUtils.success(userVos);
    }
    /**
     * 加入队伍
     * @Author:XueYuan
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinQuery teamJoinQuery,HttpServletRequest request){
        if (teamJoinQuery == null){
            throw new BusinsessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result =teamService.joinTeam(teamJoinQuery,request);
        return ResultUtils.success(result);
    }
    /**
     * 退出队伍
     * @Author:XueYuan
     */
    @PostMapping("/exit")
    public BaseResponse<Boolean> exitTeam(@RequestBody TeamQueryRequest teamQueryRequest,HttpServletRequest request){
        if (teamQueryRequest == null){
            throw new BusinsessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teamService.exitTeam(teamQueryRequest,request);
        return ResultUtils.success(result);
    }
    /**
     * 解散队伍
     * @Author:XueYuan
     */
    @PostMapping("/dismiss")
    public BaseResponse<Boolean> dismissTeam(@RequestBody TeamQueryRequest teamQueryRequest,HttpServletRequest request){
        if (teamQueryRequest == null){
            throw new BusinsessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = teamService.dismissTeam(teamQueryRequest,request);
        return ResultUtils.success(result);
    }
    /**
     * 用户匹配
     * @Author:XueYuan
     */
    @GetMapping("/match")
    public BaseResponse<List<UserVo>> matchTags(long num,HttpServletRequest request){
        if (num <= 0){
            throw new BusinsessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        List<UserVo> list = teamService.matchTags(num,loginUser);
        return ResultUtils.success(list);
    }
}
